import 'dart:io';

import 'package:flutter/material.dart';
import 'package:carousel_slider/carousel_slider.dart';
import 'package:get/get.dart';
import 'package:zhaoxiansheng_app/model/Person.dart';
import 'package:zhaoxiansheng_app/pages/test.dart';
import 'package:get_storage/get_storage.dart';

class Home extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return HomeState();
  }
}

class HomeState extends State<Home> {
  Rx<Person?> userInfo = Rx(null);
  @override
  void initState() {
    var storage = GetStorage();
    Map<String,dynamic>? map = storage.read("userInfo");
    if(map!=null){
      userInfo.value = Person.fromMap(map);
    }
    else {
      userInfo.value = null;
    }
    print("收到的userInfo:${userInfo}");
    print("HomeInit可以发送一些请求");
  }

  final List<String> options = ["精选", "日漫", "剧场版", "国漫", "欧美"];
  int selected_title = 0; // 当前选中的索引
  TextEditingController text_controller = TextEditingController();
  PageController _pageController = PageController();
  int _currentPage = 0;
  List<String> picturesList = [
    "assests/1.png",
    "assests/2.png",
    "assests/3.jpg",
    "assests/4.jpg",
    "assests/5.jpg",
  ];
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SingleChildScrollView(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,

          children: [
            SizedBox(height: 20),
            Row(
              children: [
                CircleAvatar(
                  // backgroundColor: Colors.blue,
                  radius: 20,
                  child: Obx(() {
                    if (userInfo.value == null) {
                      return Container(
                        color: Colors.blueAccent,
                        child: Text("登录"),
                      );
                      // Text("登录", style: TextStyle(color: Colors.black));
                    } else {
                      return CircleAvatar(
                        backgroundImage: FileImage(
                          File(userInfo.value!.imageUrl as String),
                        ),
                      );
                      // Image.file(File(userInfo.value!.imageUrl as String));
                    }
                  }),
                  // backgroundImage: FileImage(File(userInfo.value!.imageUrl as String)),
                ),
                SizedBox(width: 20),
                Expanded(
                  child: TextField(
                    controller: text_controller,
                    decoration: InputDecoration(
                      hintText: "今天想看点什么",
                      // 减小内边距，让文字更靠近输入框的边缘
                      contentPadding: EdgeInsets.symmetric(
                        horizontal: 0,
                        vertical: 0,
                      ),
                      hintStyle: TextStyle(fontSize: 14), // 稍微增大提示文字大小
                      prefixIcon: Icon(Icons.search),
                      border: OutlineInputBorder(
                        borderRadius: BorderRadius.all(Radius.circular(20)),
                      ),
                      // isDense: true, // 尝试设置为true，使输入框更紧凑
                    ),
                    style: TextStyle(fontSize: 14), // 确保输入文字的大小也合适
                  ),
                ),
              ],
            ),
            // SizedBox(height: 20),
            Container(
              height: 50,
              child: ListView.builder(
                scrollDirection: Axis.horizontal,
                itemCount: options.length,
                itemBuilder: (context, index) {
                  bool isSelected = index == selected_title;
                  return GestureDetector(
                    onTap: () {
                      setState(() {
                        selected_title = index;
                      });
                    },
                    child: Container(
                      alignment: Alignment.center,
                      margin: EdgeInsets.symmetric(horizontal: 8),
                      padding: EdgeInsets.symmetric(
                        horizontal: 16,
                        vertical: 8,
                      ),
                      decoration: BoxDecoration(
                        border: Border(
                          bottom: BorderSide(
                            color: isSelected
                                ? Colors.blue
                                : Colors.transparent,
                            width: 2, // 下边框高度
                          ),
                        ),
                      ),
                      child: Text(
                        options[index],
                        style: TextStyle(
                          color: isSelected ? Colors.blue : Colors.grey,
                          fontWeight: isSelected
                              ? FontWeight.bold
                              : FontWeight.normal,
                        ),
                      ),
                    ),
                  );
                },
              ),
            ),
            SizedBox(height: 20),
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Text(
                  "热门新番",
                  style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
                ),
                Icon(Icons.arrow_forward_sharp, size: 16), // 右边箭头
              ],
            ),
            // SizedBox(height: 10),
            CarouselSlider(
              options: CarouselOptions(
                height: 200,
                autoPlay: true, // 自动轮播
                enlargeCenterPage: true,
                viewportFraction: 0.9,
                aspectRatio: 16 / 9,
                enableInfiniteScroll: true, // 无限循环
              ),
              items: List.generate(5, (index) {
                return Container(child: Image.asset(picturesList[index]));
              }),
            ),
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Text(
                  "日漫",
                  style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
                ),
                Icon(Icons.arrow_forward_sharp, size: 16), // 右边箭头
              ],
            ),
            // HighlightListSimple(),
            GridView.builder(
              shrinkWrap: true,
              itemCount: 9,
              physics: NeverScrollableScrollPhysics(),
              gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                crossAxisCount: 3,
                crossAxisSpacing: 8,
                mainAxisSpacing: 8,
                childAspectRatio: 3 / 4,
              ),
              itemBuilder: (context, index) {
                return Container(
                  decoration: BoxDecoration(
                    color: Colors.blue, // 背景颜色
                    borderRadius: BorderRadius.all(
                      Radius.circular(12), // 圆角半径
                    ),
                    image: DecorationImage(
                      image: AssetImage('assests/${index + 1}.webp'),
                    ),
                  ),
                  width: 200,
                  height: 200,
                  // color: Colors.blue,
                );
              },
            ),
          ],
        ),
      ),
    );
  }
}
